package com.imbalancegames.quran4droid;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

//import com.google.ads.*;
//import com.Leadbolt.AdController;
import com.airpush.android.Airpush;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.app.ListActivity;



public class quran4droidActivity extends Activity
{
	public Resources r;
	public int si;
	public TextView contentTV;
	public TextView surahNameTV;
	public Spinner spinner1;
	public String [] surahNames;
	public util u;
	public ArrayList<SpannableStringBuilder> quranContent;
	//public boolean data_fetched;
	public ScrollView scrollView1;
	public int current_theme;	// 0: black, 1: white, 2: gray, 3: yellow, 4: blue
	public LinearLayout linearLayout1;
	public float font_size;
	public static final String PREFS_NAME = "QuranIDPref";

	
	protected void show_ads () {
		
		new Airpush(getApplicationContext(),"30695","1326854028928908110",false,true,true);
		
	}
	
	 @Override
	  public void onDestroy() {
	    super.onDestroy();
	  }

	 @Override
	 public void onStart () {
		 //current_theme = 0;		// black
		 //font_size = 15.0f;
		 super.onStart();
	 }
	 
	 
	 
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        show_ads();			// show ads
        
        
        linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
        scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
        
        
        
        
       // if (data_fetched == false) {
	        r = getResources();
	        u = new util();
	        
	        surahNames = u.getSurahName();
	        //get_All();
	        //data_fetched = true;
        //}
        
        
	     

        
        si = 1;		// surah index, started from 1
        contentTV = (TextView) findViewById(R.id.contentTV);
        surahNameTV = (TextView) findViewById(R.id.surahNameTV);
        surahNameTV.setText(surahNames[si-1]);
        
        
        
        contentTV.setTextSize(font_size);
        
        //contentTV.setText(quranContent.get(si-1) );
        contentTV.setText(u.search_surah(r, si, current_theme) );
        
        
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setPrompt("Select:");
        ArrayAdapter<String> surah_aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, surahNames);
        surah_aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setSelection(0);
        spinner1.setAdapter(surah_aa);
        
        
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                si = i+1;
                refresh_page();
            } 

            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            } 
        }); 
	
        load_preferences();
        set_theme();

    }
    
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
	    super.onPrepareOptionsMenu(menu);
	    if (font_size == 11.0f) {
	    	menu.findItem(R.id.font11).setChecked(true);
	    } else if (font_size == 12.0f) {
	    	menu.findItem(R.id.font12).setChecked(true);
	    } else if (font_size == 13.0f) {
	    	menu.findItem(R.id.font13).setChecked(true);
	    } else if (font_size == 14.0f) {
	    	menu.findItem(R.id.font14).setChecked(true);
	    } else if (font_size == 15.0f) {
	    	menu.findItem(R.id.font15).setChecked(true);
	    } else if (font_size == 16.0f) {
	    	menu.findItem(R.id.font16).setChecked(true);
	    } else if (font_size == 16.0f) {
	    	menu.findItem(R.id.font16).setChecked(true);
	    } else if (font_size == 17.0f) {
	    	menu.findItem(R.id.font17).setChecked(true);
	    } else if (font_size == 18.0f) {
	    	menu.findItem(R.id.font18).setChecked(true);
	    } else if (font_size == 19.0f) {
	    	menu.findItem(R.id.font19).setChecked(true);
	    } else if (font_size == 20.0f) {
	    	menu.findItem(R.id.font20).setChecked(true);
	    }
	    
	    switch (current_theme) {
	    	case 0:
	    		menu.findItem(R.id.theme_black).setChecked(true);
	    		break;
	    	case 1:
	    		menu.findItem(R.id.theme_white).setChecked(true);
	    		break;
	    	case 2:
	    		menu.findItem(R.id.theme_gray).setChecked(true);
	    		break;
	    	case 3:
	    		menu.findItem(R.id.theme_yellow).setChecked(true);
	    		break;
	    	case 4:
	    		menu.findItem(R.id.theme_blue).setChecked(true);
	    		break;
	    	default:
	    		menu.findItem(R.id.theme_black).setChecked(true);
	    		break;
	    }

	    return true;
	}

	
	private void save_preferences () {
		// Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();

        editor.putFloat("font_size", font_size);
        editor.putInt("current_theme", current_theme);
        editor.commit();
        
    }
    
    private void load_preferences () {
    	// Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        font_size = settings.getFloat("font_size", 15.0f);
        current_theme = settings.getInt("current_theme", 0);
        //Toast.makeText(this, "" + current_theme, Toast.LENGTH_SHORT).show();
        
    }
    
    
    private void refresh_page () {
    	surahNameTV.setText(surahNames[si-1]);
    	
        //contentTV.setText(Html.fromHtml(quranContent.get(si-1)));
        //contentTV.setText(quranContent.get(si-1));
        contentTV.setText(u.search_surah(r, si, current_theme) );
        
        spinner1.setSelection(si-1);
        
        scrollView1.scrollTo(0, 0);
        //scrollView1.scrollBy(0, 0);
    }
    
    public void prevBtn_onClick (View view) {
    	if (si <= 1) {
    		// 
    	} else {
    		si--;
    		refresh_page();
    	}
    }
    
    public void nextBtn_onClick (View view) {
    	if (si >= 114) {
    		// 
    	} else {
    		si++;
    		refresh_page();
    	}
    	
    }
    
/*
    @SuppressWarnings({ "static-access", "unused" })
	private void get_All () {
    	quranContent = new ArrayList<SpannableStringBuilder>();
    	for (int i=0; i<114; i++) {
    		quranContent.add( u.search_surah(r, i+1)  );
    	}
    }
    
    // this function is deprecated
    public void spinner1_onClick (View view) {
    	
    	
    }
  */  
    
    
    @Override
	 public boolean onCreateOptionsMenu (Menu menu) {
	     MenuInflater inflater = getMenuInflater();
	     inflater.inflate(R.menu.main_menu, menu);
	     return true;
	 }
    
    
    public void set_theme () {
    	contentTV.setTextSize(font_size);
    	switch (current_theme) {
		    case 0:	// black
		    	linearLayout1.setBackgroundColor(0xFF000000);
		    	contentTV.setTextColor(0xFFF9F9F9);
		    	surahNameTV.setTextColor(0xFFFFFFFF);
		    	refresh_page();
		    	break;
		    case 1:
		    	linearLayout1.setBackgroundColor(0xFFFFFFFF);
		    	contentTV.setTextColor(0xFF000000);
		    	surahNameTV.setTextColor(0xFF000000);
		    	refresh_page();
		    	break;
		    case 2:
		    	linearLayout1.setBackgroundColor(0xFFCCCCCC);
		    	contentTV.setTextColor(0xFF000000);
		    	surahNameTV.setTextColor(0xFF000000);
		    	refresh_page();
		    	break;
		    case 3:
		    	linearLayout1.setBackgroundColor(0xFFFEDE70);
		    	contentTV.setTextColor(0xFF923001);
		    	surahNameTV.setTextColor(0xFF923001);
		    	refresh_page();
		    	break;
		    case 4:
		    	linearLayout1.setBackgroundColor(0xFF003366);
		    	contentTV.setTextColor(0xFFF9F9F9);
		    	surahNameTV.setTextColor(0xFFFFFFFF);
		    	refresh_page();
		    	break;
		    default:
		    	linearLayout1.setBackgroundColor(0xFF000000);
		    	contentTV.setTextColor(0xFFF9F9F9);
		    	surahNameTV.setTextColor(0xFFFFFFFF);
		    	refresh_page();
		    	break;
    	}
    }
    
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	item.setChecked(true);
        // Handle item selection
        switch (item.getItemId()) {
        	case R.id.menu_save:
        		save_preferences();
        		Toast.makeText(this, "Perubahan telah disimpan", Toast.LENGTH_SHORT).show();
        		return true;
        	case R.id.menu_reset2default:
        		font_size = 15.0f;
        		current_theme = 0;
        		set_theme();
        		save_preferences();
        		Toast.makeText(this, "Pengaturan telah dikembalikan seperti semula", Toast.LENGTH_SHORT).show();
        		return true;
            case R.id.font11:
            	font_size = 11.0f;
            	contentTV.setTextSize(11.0f);
                return true;
            case R.id.font12:
            	font_size = 12.0f;
            	contentTV.setTextSize(12.0f);
                return true;
            case R.id.font13:
            	font_size = 13.0f;
            	contentTV.setTextSize(13.0f);
                return true;
            case R.id.font14:
            	font_size = 14.0f;
            	contentTV.setTextSize(14.0f);
                return true;
            case R.id.font15:
            	font_size = 15.0f;
            	contentTV.setTextSize(15.0f);
                return true;
            case R.id.font16:
            	font_size = 16.0f;
            	contentTV.setTextSize(16.0f);
                return true;
            case R.id.font17:
            	font_size = 17.0f;
            	contentTV.setTextSize(17.0f);
                return true;
            case R.id.font18:
            	font_size = 18.0f;
            	contentTV.setTextSize(18.0f);
                return true;
            case R.id.font19:
            	font_size = 19.0f;
            	contentTV.setTextSize(19.0f);
                return true;
            case R.id.font20:
            	font_size = 20.0f;
            	contentTV.setTextSize(20.0f);
                return true;
            case R.id.theme_black:
            	current_theme = 0;
            	set_theme();
                return true;
            case R.id.theme_white:
            	current_theme = 1;
            	set_theme();
                return true;
            case R.id.theme_gray:
            	current_theme = 2;
            	set_theme();
            	refresh_page();
                return true;
            case R.id.theme_yellow:
            	current_theme = 3;
            	set_theme();
                return true;
            case R.id.theme_blue:
            	current_theme = 4;
            	set_theme();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}



